package com.example.qibla_v2;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class CompassView extends ImageView {

	private float direction;
	private float bearing;
	
	Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	Rect destRect = new Rect();
	
	public CompassView(Context context) {
		super(context);
	}

	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CompassView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/*@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(
				MeasureSpec.getSize(widthMeasureSpec),
				MeasureSpec.getSize(heightMeasureSpec));
	}*/

	@Override
	protected void onDraw(Canvas canvas) {
		/* This will draw the compass bas image*/
		/*super.onDraw(canvas);*/

		int w = getMeasuredWidth();
		int h = getMeasuredHeight();
		int r;
		if(w > h){
			r = h/2;
		}else{
			r = w/2;
		}
		
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(5);
		/*paint.setColor(Color.BLUE);
		canvas.drawCircle(w/2, h/2, r, paint);*/

		/*paint.setColor(Color.RED);
		canvas.drawLine(
				w/2,
				h/2,
				w/2 + r * FloatMath.sin(-direction),
				h/2 - r * FloatMath.cos(-direction),
				paint);*/
		
		Bitmap compassBase = BitmapFactory.decodeResource(getResources(), R.drawable.ic_compass_base);
		/*canvas.drawBitmap(compassBase, 0 , 0, null);*/
		
		
		Matrix transform = new Matrix();
	    transform.setTranslate(0, 0);
	    transform.preRotate((float) Math.toDegrees(-direction), w/2, h/2);
	    canvas.drawBitmap(compassBase, transform, null);
	    
	    Bitmap compassArrow = BitmapFactory.decodeResource(getResources(), R.drawable.ic_compass_arrow);
	    canvas.drawBitmap(compassArrow, w/2-16 ,-4 , null);
		/*canvas.rotate((float) Math.toDegrees(-direction),w/2,h/2);*/
	    
	    /*paint.setColor(Color.BLACK);
	    canvas.drawText(""+Math.toDegrees(-direction), w/2, h/2 , paint);*/
		
	    Bitmap kabba = BitmapFactory.decodeResource(getResources(), R.drawable.ic_kaaba);
	    canvas.drawBitmap(kabba, w/2 ,-4 , null);
	    
		paint.setColor(Color.GREEN);
		canvas.drawLine(
				w/2,
				h/2,
				w/2 + r * FloatMath.sin(bearing - direction),
				h/2 - r * FloatMath.cos(bearing - direction),
				paint);

		
	}

	public void update(float dir, float brng){
		Log.i("QiblaTag", "inside update method");

		direction = dir;
		bearing = brng;

		Log.i("QiblaTag", "north direction = "  + direction);
		Log.i("QiblaTag", "bearing to north = " + bearing);
		
		/* From Android API reference: If the view is visible, OnDraw will be called in the future accordingly*/
		invalidate();
	}
	
	public static Bitmap drawableToBitmap (Drawable drawable, Canvas canvas) {
	    if (drawable instanceof BitmapDrawable) {
	        return ((BitmapDrawable)drawable).getBitmap();
	    }

	    int width = drawable.getIntrinsicWidth();
	    width = width > 0 ? width : 1;
	    int height = drawable.getIntrinsicHeight();
	    height = height > 0 ? height : 1;

	    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
	    /*Canvas canvas = new Canvas(bitmap); */
	    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
	    drawable.draw(canvas);

	    return bitmap;
	}

}



